package cn.shyouchi.wuguanjia.android.ui.event.labor;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.ApplyLaborEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ApplyLaborActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_sel_person)
    TextView mTvSelPerson;
    @BindView(R.id.tv_sel_person_value)
    TextView mTvSelPersonValue;
    @BindView(R.id.iv_post_has_next1)
    ImageView mIvPostHasNext1;
    @BindView(R.id.item_sel_person)
    RelativeLayout mItemSelPerson;
    @BindView(R.id.tv_sel_rank)
    TextView mTvSelRank;
    @BindView(R.id.tv_sel_rank_value)
    TextView mTvSelRankValue;
    @BindView(R.id.iv_post_has_next2)
    ImageView mIvPostHasNext2;
    @BindView(R.id.item_sel_rank)
    RelativeLayout mItemSelRank;
    @BindView(R.id.tv_sel_time)
    TextView mTvSelTime;
    @BindView(R.id.tv_sel_time_value)
    TextView mTvSelTimeValue;
    @BindView(R.id.iv_post_has_next)
    ImageView mIvPostHasNext;
    @BindView(R.id.item_sel_post)
    RelativeLayout mItemSelPost;
    @BindView(R.id.edit_reason)
    EditText mEditReason;


    List<String> mDiffList = new ArrayList<>();

    String projectId;
    String date;


    List<PersonEntity> persons = new ArrayList<>();
    List<LaborRequestEntity.ResultMessageBean.ServiceBeanX> ranks = new ArrayList<>();
    PersonEntity mPersonEntity;
    LaborRequestEntity.ResultMessageBean.ServiceBeanX rank;
    String day;



    @Override
    public int initContentView() {
        return R.layout.activity_apply_labor;
    }

    @Override
    public void initInjector() {

        mDiffList.add("1");
        mDiffList.add("2");
        mDiffList.add("3");

        persons = getIntent().getParcelableArrayListExtra("persons");
        ranks = getIntent().getParcelableArrayListExtra("ranks");
        projectId = getIntent().getStringExtra("projectId");
        date = getIntent().getStringExtra("date");


        LogUtil.json(persons);
        LogUtil.json(ranks);




    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        if (persons != null) {
            mTvSelPersonValue.setText(String.format("共有%d个人员", persons.size()));
        }

        if (ranks != null) {
            mTvSelRankValue.setText(String.format("共有%d个班次", ranks.size()));

        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {
            generateEntityExit();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void generateEntityExit() {


        if (mPersonEntity == null) {
            ToastUtil.showToast("请选择人员");

            return;
        }


        if (rank == null) {
            ToastUtil.showToast("请选择班次");
            return;
        }

        if (TextUtils.isEmpty(day)) {
            ToastUtil.showToast("请选择天数");
            return;
        }

        EventApi eventApi = new EventApi(mContext);
        eventApi.applyLabor(projectId, rank.getService().getId(), day, mPersonEntity.getId(), mEditReason.getText().toString(), date)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        EventBus.getDefault().post(new ApplyLaborEvent());
                        finish();
                    }
                });
    }


    @OnClick({R.id.item_sel_person, R.id.item_sel_rank, R.id.item_sel_post})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_sel_person:
                showSelDialog("选择人员", 0);
                break;
            case R.id.item_sel_rank:
                showSelDialog("选择班次", 1);
                break;
            case R.id.item_sel_post:
                showSelDialog("选择工时(天)", 2);
                break;
        }
    }


    private synchronized void showSelDialog(String title, final int type) {


        List<String> c = new ArrayList<>();
        if (type == 0) {
            for (PersonEntity e : persons) {
                c.add(e.getName());
            }
        } else if (type == 1) {
            for (LaborRequestEntity.ResultMessageBean.ServiceBeanX s : ranks) {
                c.add(s.getService().getServicesname());
            }
        } else if (type == 2) {
            c.addAll(mDiffList);
        }

        final String[] choices = c.toArray(new String[c.size()]);


        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, AlertDialog.THEME_HOLO_LIGHT);
        TextView tvTitle = (TextView) getLayoutInflater().inflate(R.layout.dialog_title, null);
        tvTitle.setText(title);
        builder.setCustomTitle(tvTitle);
        builder.setItems(choices, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (type) {
                    case 0:

                        mPersonEntity = persons.get(which);

                        mTvSelPersonValue.setText(mPersonEntity.getName());
                        mTvSelPersonValue.setTextColor(getResources().getColor(R.color.mainColor));

                        break;
                    case 1:
                        rank = ranks.get(which);
                        mTvSelRankValue.setText(rank.getService().getServicesname());
                        mTvSelRankValue.setTextColor(getResources().getColor(R.color.mainColor));
                        break;
                    case 2:
                        day = mDiffList.get(which);
                        mTvSelTimeValue.setText(day + "天");
                        mTvSelTimeValue.setTextColor(getResources().getColor(R.color.mainColor));
                        break;
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.create().show();
    }
}
